package com.version1_3_1.controller;

import com.alibaba.fastjson.JSONObject;
import com.version1_3_1.service.ActivityServiceFixed;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by reeveslee on 16/4/20.
 */
@Controller
public class ActivityController1_3_1 {
    private static final Logger LOGGER = Logger.getLogger(ActivityController1_3_1.class);

    @Autowired
    private ActivityServiceFixed activityServiceFixed;

    /**
     * 跬步+ 公益跑打卡界面新的数据格式
     *
     * @param activityId
     * @param userId
     * @return
     */
    @RequestMapping(value = "/version1_3_1/activity/activityPage/{activityId}/{userId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String activityPage(@PathVariable("activityId") int activityId, @PathVariable("userId") int userId) {

        return activityServiceFixed.activityPage(activityId, userId);
    }

    /**
     * 打卡 ok
     * 调用接口: http://139.129.18.232:7080/kuibuversion1/activity/launch
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/version1_3_1/activity/launch", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String launchActivity(@RequestBody JSONObject jsonObject) {

        int activityId = 0;
        int userId = -1;
        try {
            Object data = jsonObject.get("data");
            activityId = jsonObject.getIntValue("activityId");
            userId = jsonObject.getIntValue("userId");

            return activityServiceFixed.launchActivity(activityId, userId, data);

        } catch (Exception exception) {
            LOGGER.error("接收客户端活动打卡信息错误,发送打卡信息: " + jsonObject.toJSONString(), exception);
            return "{\"launchResult\":false}";
        }
    }

    /**
     * 打卡 ok
     * 调用接口: http://139.129.18.232:7080/kuibuversion1/activity/launch
     *
     * @param mile
     * @return
     */
    @RequestMapping(value = "/version1_3_1/activity/inputMile/{mile}/{userId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String inputMile(@PathVariable("mile") double mile, @PathVariable("userId") int userId) {

        return activityServiceFixed.inputMile(mile, userId);
    }
}
